
import pygame,sys,os
class Door():
    def __init__(self, pos_x, pos_y):
        self.image = pygame.image.load(self.resource_path("source/img/door.png"))
        self.rect = self.image.get_rect()
        self.rect.bottomleft = (pos_x, pos_y)
        self.init_x = pos_x
        self.init_y = pos_y
        self.activate = False
    def Door_display(self, window):
        window.blit(self.image, self.rect)
    def door_move(self,person,dire,speed,length,check):
        horizontal_distance = abs(person.rect.centerx - self.rect.centerx)
        person_bottom = person.rect.bottom
        #判断 人物的底部是否在（墙壁，墙壁加上人物高度）之间
        vertical_condition = (person_bottom <= self.rect.bottom) and (person_bottom >= self.rect.top - person.rect.height)

        if horizontal_distance < check and vertical_condition:
            self.activate = True

        if self.activate == True:
            if dire=='x':
                self.rect.x += speed
                if speed>0:
                    self.rect.x = min(self.rect.x,self.init_x+length)
                else:
                    self.rect.x = max(self.rect.x, self.init_x - length)
            elif dire=='y':
                self.rect.y += speed
                if speed>0:
                    self.rect.y = min(self.rect.y,self.init_y+length)
                else:
                    self.rect.y = max(self.rect.y, self.init_y - length)

    def resource_path(self,relative_path):
        """ 根据打包后的环境动态加载资源路径 """
        if hasattr(sys, '_MEIPASS'):
            base_path = sys._MEIPASS
        else:
            base_path = os.path.abspath(".")
        return os.path.join(base_path, relative_path)

